package com.ljk.util.pdf.template;

import cn.hutool.core.collection.ListUtil;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfDiv;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.ljk.util.pdf.PdfUtil;
import com.ljk.util.pdf.data.BasePresTemplate;
import com.ljk.util.pdf.data.PrescriptionItem;
import com.ljk.util.pdf.data.TcmPrescription;
import com.ljk.util.string.StringUtil;

import java.util.ArrayList;
import java.util.List;

import static com.ljk.util.pdf.PdfUtil.createCell;
import static com.ljk.util.pdf.PdfUtil.parseOneCell;

public class PdfTemplate4Zy extends BasePresTemplate<TcmPrescription> {

    @Override
    public int convertItem(BaseFont baseFont, TcmPrescription pres, PdfPTable presTable, float[] leftRightWidth) {

        Font font = new Font(baseFont, 10, Font.NORMAL);
        Font smallFont = new Font(baseFont, 7, Font.NORMAL);
        float[] onRelativeWidths = {13, 5};

        List<PrescriptionItem> presList = pres.getPresList();

        ArrayList<PdfPCell> cellList = new ArrayList<>(presList.size() / 4 * 4 + 1);
        for (PrescriptionItem drug : presList) {
            String drugName = drug.getItemName();
            String fryingTypeStr = drug.getFryingType();
            String quantity = drug.getQuantity() + drug.getQuantityUnit();
            PdfPCell cellLeft = new PdfPCell();
            if (StringUtil.isNotEmpty(fryingTypeStr)) {
                PdfDiv pdfDiv = new PdfDiv();
                pdfDiv.addElement(new Chunk(fryingTypeStr, smallFont));
                pdfDiv.setPaddingTop(-3);
                pdfDiv.setPaddingBottom(-5);
                if (StringUtil.isNotEmpty(fryingTypeStr)) {
                    int rightLength = 71 - fryingTypeStr.length() * 7;
                    int leftLength = drugName.length() * 10;
                    if (leftLength > rightLength) {
                        leftLength = rightLength;
                    }
                    pdfDiv.setPaddingLeft(leftLength);
                }
                cellLeft.addElement(pdfDiv);
            }

            PdfDiv fryingDiv = new PdfDiv();
            fryingDiv.addElement(new Chunk(drugName, font));
            cellLeft.addElement(fryingDiv);
            cellLeft.setBorder(Rectangle.NO_BORDER);
            PdfPCell cell = parseOneCell(onRelativeWidths, cellLeft, createCell(font, quantity, Element.ALIGN_LEFT));
            cellList.add(cell);
        }
        int autoCount = cellList.size() % 4;
        if (autoCount != 0) {
            PdfPCell cell = createCell(font, "", Element.ALIGN_CENTER);
            autoCount = 4 - autoCount;
            for (int i = 0; i < autoCount; i++) {
                cellList.add(cell);
            }
        }

//        PdfPTable lineTable = new PdfPTable(4);
//        for (PdfPCell cell : cellList) {
//            lineTable.addCell(cell);
//        }

//        presTable.addCell(PdfUtil.parseOneCell(new float[]{4, 96}, leftCell, PdfUtil.parseCell4Table(presTable)));
        PdfPCell leftCell = PdfUtil.createBankCell();
        List<List<PdfPCell>> splitList = ListUtil.split(cellList, 4);
        for (List<PdfPCell> pdfPCells : splitList) {
            presTable.addCell(PdfUtil.parseOneCell(leftRightWidth, leftCell, PdfUtil.parseOneCell(pdfPCells)));
        }

//        presTable.addCell(PdfUtil.parseCell4Table(lineTable));

//        String frequencyName = pres.getFrequencyName();
//        String tcmNumberAgent = pres.getTcmNumberAgent() == null ? null : "共" + pres.getTcmNumberAgent() + "剂";
//        String fryingTypeStr = pres.getUsageStr();
//        String remark = joinParam(",", tcmNumberAgent, frequencyName, fryingTypeStr);
        String fryingTypeStr = pres.getUsageStr();
        fryingTypeStr = fryingTypeStr == null ? "无" : fryingTypeStr;
        presTable.addCell(PdfUtil.parseOneCell(leftRightWidth, leftCell, createCell(font, "用法: " + fryingTypeStr, Element.ALIGN_LEFT)));
        if (StringUtil.isNotEmpty(pres.getQuickPrintStr())) {
            presTable.addCell(PdfUtil.parseOneCell(leftRightWidth, leftCell, createCell(font, pres.getQuickPrintStr(), Element.ALIGN_LEFT)));
        }
        return cellList.size() / 4;
    }




}
